package com.zlm.user.emp.service.impl;

import com.zlm.common.base.AbstractBaseService;
import com.zlm.common.base.PageEntity;
import com.zlm.common.constants.CodeConstants;
import com.zlm.common.constants.CommonConstants;
import com.zlm.common.dto.userEmp.PositionResponseDto;
import com.zlm.common.entity.EmpPostCollectDO;
import com.zlm.common.enums.*;
import com.zlm.user.emp.dao.EmpPostCollectDao;
import com.zlm.user.emp.service.EmpPostCollectService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: duyufu
 * @Date: 2019/4/30 15:51
 */
@Service
public class EmpPostCollectServiceImpl extends AbstractBaseService<EmpPostCollectDao, EmpPostCollectDO> implements EmpPostCollectService {
    @Autowired
    @Override
    public void setBaseDao(EmpPostCollectDao empPostCollectDao) {
        this.baseDao = empPostCollectDao;
    }

    @Override
    public String saveEmpPostCollect(Integer operationCode, Integer pracId, Integer empId) {
        if (operationCode.intValue() == 1) {
            //收藏职位
            int count = baseDao.countEmpPostCollect(pracId,empId);
            if (count > 0) {
                return CodeConstants.CODE_10008;
            }
            baseDao.saveEmpPostCollect(pracId, empId);
        }else if (operationCode.intValue() == 0) {
            //取消收藏
            baseDao.deleteEmpPostCollect(pracId, empId);
        }else {
            return CodeConstants.CODE_PARAMS;
        }
        return CodeConstants.CODE_SUCCESS;
    }

    @Override
    public PageEntity queryEmpPostCollect(Integer empId, Integer pageSize, Integer pageNumber) {
        int count = baseDao.countEmpPostCollect(null,empId);
        if (null == pageSize || pageSize >= CommonConstants.maxPageSize) {
            pageSize = CommonConstants.pageSize;
        }
        if (null == pageNumber) {
            pageNumber = CommonConstants.pageNumber;
        }else {
            pageNumber = (pageNumber - 1) * pageSize;
        }
        List<PositionResponseDto> empPostCollectList = baseDao.queryEmpPostCollect(empId,pageSize,pageNumber);
        for (PositionResponseDto paramsDto : empPostCollectList) {
            if (StringUtils.isNotBlank(paramsDto.getAttUrl())) {
                paramsDto.setAttUrl(CommonConstants.IMG_URL.concat(paramsDto.getAttUrl()));
            }
            paramsDto.setEducationName(EducationEnum.getNameByCode(paramsDto.getEducation()));
            paramsDto.setIsSuplifeName(IsSuplifeEnum.getNameByCode(paramsDto.getIsSuplife()));
            paramsDto.setWorkTimeName(ExperienceEnum.getNameByCode(paramsDto.getWorkTime()));
            paramsDto.setStdSalLoopName(StdSalLoopEnum.getNameByCode(paramsDto.getStdSalLoop()));
            paramsDto.setSalary(paramsDto.getSalaryMin().toString().concat("-").concat(paramsDto.getSalaryMax().toString()).concat(paramsDto.getStdSalLoopName()));
//            if (paramsDto.getPostStyle() != 0) {
//                paramsDto.setPostStyleName(PositionStyleEnum.getNameByCode(paramsDto.getPostStyle()));
//            }
            paramsDto.setIsTravelExpensesName(IsTravelEnum.getNameByCode(paramsDto.getIsTravelExpenses()));
        }
        PageEntity page = getPage(pageNumber,pageSize,count,empPostCollectList);
        return page;
    }
}
